package com.fresh.liangyi.dao;

import com.fresh.liangyi.model.domain.Goods;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

@Mapper
@Component
public interface GoodsMapper {
    //商品Goods的增删改查

    //添加商品
    @Options(useGeneratedKeys = true,keyProperty="id")//返回主键到实体类中
    @Insert("INSERT INTO goods(title,introduce,price,discount,create_date,sales_volume,stock,image,category_id) " +
            "VALUES(#{title},#{introduce},#{price},#{discount,jdbcType=DECIMAL},#{createDate},#{salesVolume},#{stock},#{image},#{categoryId})")
    public int addGoods(Goods goods);

    //浏览全部商品(根据销量排行）
    @Select("SELECT * FROM goods ORDER BY sales_volume DESC")
    public List<Goods> selectGoodsAll();

    //==根据参数获取商品==
    //By Id
    @Select("SELECT * FROM goods WHERE id=#{id}")
    public Goods selectById(Integer id);
    //By 关键词（搜索）
    @Select("SELECT * FROM goods WHERE title like concat('%',#{keyword},'%') OR " +
            "introduce like concat('%',#{keyword},'%') ORDER BY sales_volume DESC ")
    public List<Goods> selectByKeyword(String keyword);

    //By CategoryId
    @Select("SELECT * FROM goods WHERE category_id=#{cid}")
    public List<Goods> selectByCategoryId(Integer id);

    //删除商品
    @Delete("DELETE * FROM goods WHERE id=#{id}")
    public int deleteGoods(Integer id);

    //更改商品
    public Goods updateGoods(Goods goods);

//    查询购物车
    @Select("SELECT g.* FROM `shoping_cart` c JOIN `goods` g ON c.user_id=#{id} AND c.goods_id=g.id ")
    public List<Goods> selectCartByUserId(Integer id);
    //添加商品到购物车
    @Insert("INSERT INTO shoping_cart(user_id,goods_id) VALUES(#{userId},#{goodsId})")
    public Integer addCartGoods(Integer userId,Integer goodsId);
    //删除购物车商品
    @Delete("DELETE FROM shoping_cart  WHERE user_id=#{userId} AND goods_id=#{goodsId} ")
    public Integer deleteCart(Integer userId,Integer goodsId);
}
